package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.Market;
import com.hyk.domain.TurnoverOrder;
import com.hyk.dto.MarketDto;
import com.hyk.dto.R;
import com.hyk.feign.MarketFeign;
import com.hyk.service.MarketService;
import com.hyk.service.TurnoverOrderService;
import com.hyk.vo.DepthItemVo;
import com.hyk.vo.DepthsVo;
import com.hyk.vo.TurnoverOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "市场配置控制器")
@RequestMapping("/markets")
public class MarketController implements MarketFeign {

    @Resource
    private MarketService marketService;

    @Resource
    private TurnoverOrderService turnoverOrderService;

    @GetMapping
    @ApiOperation("根据条件查询分页")
    public R<Page<Market>> list(@ApiIgnore Page<Market> page,
                                Long tradeAreaId, /*Long coinId,*/ Byte status){
        return R.ok(marketService.page(page,new LambdaQueryWrapper<Market>().
                eq(tradeAreaId != null,Market::getTradeAreaId,tradeAreaId).
//                eq(coinId != null,Market::getSellCoinId,coinId).
//                eq(coinId != null,Market::getBuyCoinId,coinId).
                eq(status != null,Market::getStatus,status).
                orderByDesc(Market::getCreated)));
    }

    @PostMapping
    @ApiOperation("新增")
    public R add(@RequestBody Market market){
        marketService.saveMarket(market);
        return R.ok();
    }

    @PatchMapping
    public R update(@RequestBody Market market){
        marketService.updateById(market);
        return R.ok();
    }

    @PostMapping("/setStatus")
    public R status(@RequestBody Market market){
        marketService.updateById(market);
        return R.ok();
    }

    @GetMapping("/trades/{symbol}")
    @ApiOperation("查询成交记录")
    public R<List<TurnoverOrderVo>> findSymbolTurnoverOrder(@PathVariable("symbol") String symbol){
        List<TurnoverOrder> turnoverOrders = turnoverOrderService.list(new LambdaQueryWrapper<TurnoverOrder>()
        .eq(TurnoverOrder::getSymbol,symbol)
        .orderByDesc(TurnoverOrder::getCreated)
        .last("limit 20"));
        List<TurnoverOrderVo> turnoverOrderVos = turnoverOrders.stream().map(turnoverOrder -> {
            TurnoverOrderVo turnoverOrderVo = new TurnoverOrderVo();
            Date created = turnoverOrder.getCreated();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

            turnoverOrderVo.setCreated(simpleDateFormat.format(created));

            Market one = marketService.getOne(new LambdaQueryWrapper<Market>()
                    .eq(Market::getSymbol, turnoverOrder.getSymbol()));
            turnoverOrderVo.setCurrentMarket(one);
            turnoverOrderVo.setPrice(turnoverOrder.getDealBuyFee());
            turnoverOrderVo.setVolume(turnoverOrder.getBuyVolume());
            return turnoverOrderVo;
        }).collect(Collectors.toList());
        return R.ok(turnoverOrderVos);
    }

    @GetMapping("/depth/{symbol}/{dep}")
    public R<DepthsVo> depth(@PathVariable("symbol") String symbol, @PathVariable("dep") String dep) {
        DepthsVo depthsVo = new DepthsVo();

        Market market = marketService.getOne(new LambdaQueryWrapper<Market>()
                .eq(Market::getSymbol, symbol));

        //委托买单
        List<DepthItemVo> bids = new ArrayList<>();
        //todo 查询数据
        DepthItemVo bid = new DepthItemVo();
        bid.setPrice(new BigDecimal(100));
        bid.setVolume(new BigDecimal(10));
        bids.add(bid);

        //委托卖单
        List<DepthItemVo> asks = new ArrayList<>();
        DepthItemVo ask = new DepthItemVo();
        ask.setPrice(new BigDecimal(100));
        ask.setVolume(new BigDecimal(10));
        asks.add(ask);

        depthsVo.setAsks(asks);
        depthsVo.setBids(bids);

        depthsVo.setCnyPrice(market.getOpenPrice());
        depthsVo.setPrice(market.getOpenPrice());

        return R.ok(depthsVo);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有的交易市场")
    public R<List<Market>> listMarks() {
        return R.ok(marketService.list());
    }

    @Override
    @GetMapping("/getMarketBySymbol")
    public MarketDto getMarketBySymbol(String symbol) {
        Market market = marketService.getOne(new LambdaQueryWrapper<Market>().eq(Market::getSymbol, symbol));
        MarketDto marketDto = new MarketDto();
        BeanUtils.copyProperties(market,marketDto);
        return marketDto;
    }

    @Override
    public List<MarketDto> getMarketBySymbols(List<String> symbol) {
        List<Market> market = marketService.list(new LambdaQueryWrapper<Market>()
                .in(Market::getSymbol, symbol));

        return market.stream().map(market1 -> {
            MarketDto marketDto = new MarketDto();
            BeanUtils.copyProperties(market1, marketDto);
            return marketDto;
        }).collect(Collectors.toList());
    }
}
